<template>
  <div>
    <br />
    <a-card class="card-content">
      <a-table
        v-if="tableData.length > 0"
        :scroll="{ x: 1500 }"
        :columns="columns"
        :data-source="tableData"
        :loading="loading"
        childrenColumnName="children"
        :pagination="pagination"
        @change="handleTableChange"
        defaultExpandAllRows
        bordered
        :rowKey="(record, index) => record.id"
      >
        <template slot="productWork" slot-scope="text, record">
          <div
            v-for="(item, worKIndex) in record.productWorkStageList"
            :key="worKIndex"
          >
            {{ item.workStageId }} {{ item.workStageName }} 工时:{{
              item.processTime
            }}
          </div>
        </template>
        <template slot="action" slot-scope="text, record">
          <a @click="handleChild(record.id)" v-if="record.sourceName !== '外购'"
            >新增下级</a
          >
          <a-divider
            v-hasPromise="'productEdit'"
            v-if="record.sourceName !== '外购'"
            type="vertical"
          />
          <a
            v-hasPromise="'productEdit'"
            @click="handleToChild('/product/child', record)"
            >编辑</a
          >
          <a-divider
            type="vertical"
            v-if="tableData.length && record.level !== 1"
            v-hasPromise="'productDetailDel'"
          />
          <a-popconfirm
            v-if="tableData.length && record.level !== 1"
            okText="确定"
            cancelText="取消"
            v-hasPromise="'productDetailDel'"
            title="确定删除吗?"
            @confirm="() => handleDelete(record)"
          >
            <a>删除</a>
          </a-popconfirm>
        </template>
      </a-table>
      <ChildModal
        ref="Child"
        @ok="modalFormOk"
        :treeData="treeData"
      ></ChildModal>
    </a-card>
  </div>
</template>

<script>
import { columns } from "./components/tableConfig";
import { tableMinxins } from "@/unit/mixins/tableMinxins.js";
import ChildModal from "./components/ChildModal.vue";
import api from "@/api/apiServer";
export default {
  components: { ChildModal },
  mixins: [tableMinxins],
  data() {
    return {
      columns,
      tableData: [],
      treeData: [],
      Option: [],
      model: {},
    };
  },
  mounted() {
    this.queryParam.id = this.$route.query.id;
    this.getList();
    this.getTree();
  },
  methods: {
    // 获取列表数据
    async getList(arg) {
      this.loading = true;
      //加载数据 若传入参数1则加载第一页的内容
      if (arg === 1) {
        this.pagination.current = 1;
      }
      var params = this.getQueryParams();
      const res = await api.ProductDetailList(params);
      this.tableData = res.data;
      this.loading = false;
      // res.data.children.forEach((item) => {
      //   if(item.sourceName == "外购"){
      //     item.isShow = false
      //   }
      // });
    },
    modalFormOk() {
      this.queryParam.id = this.$route.query.id;
      this.getList();
    },
    // 新增下级
    handleChild(id) {
      this.$refs.Child.title = "新增";
      this.$refs.Child.modalAdd(id);
    },
    handleToChild(path, record) {
      let { id } = record;
      this.$router.push({ path, query: { id } });
    },
    // 删除
    async handleDelete(record) {
      let params = {
        id: record.id,
      };
      const res = await api.ProductDelete(params);
      this.$message.success(res.message);
      this.getList();
    },
    async getTree() {
      const res = await api.CommodityTypeFindAll();
      this.treeData = res.data;
    },
  },
};
</script>

<style lang="less" scoped>
.card-header {
  margin: 20px 0;
}
.card-content {
  button {
    margin-right: 20px;
  }
}
</style>
